//
//  BHCache.swift
//  BigHealth
//
//  Created by 布丁 on 2021/8/2.
//

import UIKit

class BHCache {
    static func save_userToken(token: String) {
        BHCache_UserDefault.saveForKey(key: "bh_user_info_token", value: token)
    }
    
    static func load_userToken() -> String {
        let token = BHCache_UserDefault.objectForKey(key: "bh_user_info_token")
        if token != nil {
            return  token as! String
        }else {
            return ""
        }
    }
    
    static func remove_token() {
        BHCache_UserDefault.removeValueForKey(key: "bh_user_info_token")
    }
    
    static func save_userSign(userSign: String) {
        BHCache_UserDefault.saveForKey(key: "bh_user_info_im_userSign", value: userSign)
    }
    
    static func load_userSign() -> String {
        let userSign = BHCache_UserDefault.objectForKey(key: "bh_user_info_im_userSign")
        if userSign != nil {
            return  userSign as! String
        }else {
            return ""
        }
    }
    
    static func remove_userSign() {
        BHCache_UserDefault.removeValueForKey(key: "bh_user_info_im_userSign")
    }
    
}

// 程序活跃和非活跃时间记录
extension BHCache {
    static func save_appActiveTime(timeValue: Int) {
        BHCache_UserDefault.saveForKey(key: "bh_application_active_time", value: timeValue)
    }
    
    static func load_appActiveTime() -> Int {
        let time = BHCache_UserDefault.objectForKey(key: "bh_application_active_time")
        if time == nil {
            return 0
        }else {
            return time as! Int
        }
    }
    
    static func save_appSleepTime(timeValue: Int) {
        BHCache_UserDefault.saveForKey(key: "bh_application_sleep_time", value: timeValue)
    }
    
    static func load_appSleepTime() -> Int {
        let time = BHCache_UserDefault.objectForKey(key: "bh_application_sleep_time")
        if time == nil {
            return 0
        }else {
            return time as! Int
        }
    }
}


class BHCache_UserDefault {
    
    // 存
    static func saveForKey(key: String, value: Any) {
        let userDefault = UserDefaults.standard
        userDefault.setValue(value, forKey: key)
        userDefault.synchronize()
    }
    // 取
    static func objectForKey(key: String) -> Any? {
        let userDefault = UserDefaults.standard
        return userDefault.object(forKey: key)
    }
    // 删
    static func removeValueForKey(key: String) {
        let userDefault = UserDefaults.standard
        if userDefault.object(forKey: key) != nil {
            userDefault.removeObject(forKey: key)
            userDefault.synchronize()
        }
    }
}

// 用户行为缓存
extension BHCache_UserDefault {
    static func save_circleChose() {
        BHCache_UserDefault.saveForKey(key: "bh_circle_chose_behavior_\(BHMeUserInfoModel.shared.userId)", value: 1)
    }
    
    static func load_circleChose() -> Int {
        let code = BHCache_UserDefault.objectForKey(key: "bh_circle_chose_behavior_\(BHMeUserInfoModel.shared.userId)")
        if code != nil {
            return code as! Int
        }else {
            return 0
        }
    }
}


